import { h } from 'vue';
import { BasicColumn, FormSchema } from '@/components/Table';
import { Switch } from 'ant-design-vue';
import { setRoleStatus } from '@/api/demo/system';
import { useMessage } from '@/hooks/web/useMessage';
import { getAllRoleList } from '@/api/demo/system';

type CheckedType = boolean | string | number;
export const columns: BasicColumn[] = [
  {
    title: '采集源名称',
    dataIndex: 'roleName',
    align: 'left',
    fixed: 'left',
  },
  {
    title: '采集源站点',
    dataIndex: 'url',
    align: 'left',
    width: 280,
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: 120,
    sorter: true,
    customRender: ({ record }) => {
      if (!Reflect.has(record, 'pendingStatus')) {
        record.pendingStatus = false;
      }
      return h(Switch, {
        checked: record.status === '1',
        checkedChildren: '停用',
        unCheckedChildren: '启用',
        loading: record.pendingStatus,
        onChange(checked: CheckedType) {
          record.pendingStatus = true;
          const newStatus = checked ? '1' : '0';
          const { createMessage } = useMessage();
          setRoleStatus(record.id, newStatus)
            .then(() => {
              record.status = newStatus;
              createMessage.success(`已成功修改角色状态`);
            })
            .catch(() => {
              createMessage.error('修改角色状态失败');
            })
            .finally(() => {
              record.pendingStatus = false;
            });
        },
      });
    },
  },
  {
    title: '创建时间',
    dataIndex: 'id',
    width: 180,
    sorter: true,
    fixed: 'right',
    customRender: ({record})=>{
      return record.createTime;
    }
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'name',
    label: '采集站点名称',
    component: 'Input',
    colProps: { xs: 24, md: 8, lg: 6 },
  },
  {
    field: 'name',
    label: '采集站点URL',
    component: 'Input',
    colProps: { xs: 24, md: 8, lg: 6 },
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'roleName',
    label: '采集源名称',
    required: true,
    component: 'Input',
  },
  {
    field: 'website',
    label: '采集源站点',
    required: true,
    component: 'Input',
  },
  {
    field: 'method',
    label: '请求方式',
    component: 'RadioButtonGroup',
    defaultValue: 'get',
    componentProps: {
      options: [
        { label: 'GET', value: 'get' },
        { label: 'POST', value: 'post' },
      ],
    },
    colProps: { xs: 24, md: 8 },
  },
  {
    field: 'dataformat',
    label: '数据格式',
    component: 'RadioButtonGroup',
    defaultValue: 'html',
    componentProps: {
      options: [
        { label: 'HTML', value: 'html' },
        { label: 'JSON', value: 'json' },
      ],
    },
    colProps: { xs: 24, md: 8 },
  },
  {
    field: 'status',
    label: '状态',
    component: 'RadioButtonGroup',
    defaultValue: '1',
    componentProps: {
      options: [
        { label: '启用', value: '1' },
        { label: '停用', value: '0' },
      ],
    },
    colProps: { xs: 24, md: 8 },
  },   
  {
    label: '备注',
    field: 'remark',
    component: 'InputTextArea',
  },
  {
    field: 'listConfig',
    label: '列表配置',
    required: true,
    component: 'Input',
    defaultValue:`{
      "Link": {
          "XPath": "div[@class='item-box_content']/h3/a",
          "Attr": "href",
          "ReplaceWords": null,
          "RemoveChildXPath": null
      },
      "Title": {
          "XPath": "div[@class='item-box_content']/h3/a",
          "Attr": "InnerText",
          "ReplaceWords": null,
          "RemoveChildXPath": null
      },
      "RemoteImgs": {
          "XPath": "div[@class='item-box_imageContainer']/a/img",
          "Attr": "src",
          "ReplaceWords": null,
          "RemoveChildXPath": null
      },
      "Summary": {
          "XPath": "div[@class='item-box_content']/div[@class='intro']",
          "Attr": "InnerText",
          "ReplaceWords": {
              "摘要:": ""
          },
          "RemoveChildXPath": null
      },
      "PreviewUrl": {
          "XPath": "div[@class='item-box_footer']/a[2]",
          "Attr": "href",
          "ReplaceWords": null,
          "RemoveChildXPath": null
      },
      "Tags": {
          "XPath": "div[@class='item-box_content']/span/a",
          "Attr": "InnerText",
          "ReplaceWords": null,
          "RemoveChildXPath": null
      }
  }`,
    colProps: { xs: 24, md: 12 },
    slot: 'listConfig',
  },
  {
    field: 'detailConfig',
    label: '详情配置',
    required: true,
    component: 'Input',
    defaultValue: `
    {
      "Title": {
          "XPath": "//h1[@class='news-title']",
          "Attr": "InnerText",
          "ReplaceWords": null,
          "RemoveChildXPath": null
      },
      "CreateTime": {
          "XPath": "/html/body/div[2]/div/div[2]/div[1]/div/p[5]",
          "Attr": "InnerText",
          "ReplaceWords": {"更新时间：":""},
          "RemoveChildXPath": null
      },
      "Description": {
          "XPath": "//div[@class='cont']",
          "Attr": "InnerHtml",
          "ReplaceWords": null,
          "RemoveChildXPath": [
              "//input[@id='isplay']"
          ]
      }
  }
    `,
    slot: 'detailConfig',
    colProps: { xs: 24, md: 12 },
  },

];
